package cn.com.dycc.meeting_assist.meeting_summary.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import cn.com.dycc.common.attach.dao.AttachDao;
import cn.com.dycc.common.attach.entity.Attach;
import cn.com.dycc.meeting_assist.meeting_summary.dao.SummaryDao;
import cn.com.dycc.meeting_assist.meeting_summary.entity.Summary;
import cn.com.dycc.meeting_assist.meeting_summary.param.FindSummaryParam;
import cn.com.dycc.meeting_assist.meeting_summary.param.SummaryParam;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.FileUtil;
import cn.com.dycc.util.Paging;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * Description: 会议纪要管理业务层实现
 * Author: Liket
 * Version: 1.0
 * Create Date Time: 2018年10月29日 上午10:16:03.
 * Update Date Time: 
 * @see
 */
@Service("summaryService")
public class SummaryServiceImpl implements SummaryService{
	
	@Resource
	private SummaryDao summaryDao;
	
	@Resource
	private AttachDao attachDao;
	
	@Resource(name="paging")
	private Paging paging;
	
		
	public int addSummary(HttpServletRequest req, Summary summary)
			throws SummaryNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		String user_id = (String)session.getAttribute("id");
		if(org_id==null){
			throw new SummaryNotFoundException("请重新登录");
		}
		if(summary==null){
			throw new SummaryNotFoundException("请填写会议室信息");
		}
		if(summary.getMeeting_id()==null||summary.getMeeting_id().trim().isEmpty()){
			throw new SummaryNotFoundException("请选择会议");
		}
		SummaryParam old_summary = summaryDao.findSummaryById(summary.getMeeting_id(),org_id);
		if(old_summary!=null){
			throw new SummaryNotFoundException("该会议纪要已经存在");
		}
		if(summary.getSummary_name()==null||summary.getSummary_name().trim().isEmpty()){
			throw new SummaryNotFoundException("请填写名称");
		}
		
		summary.setOrg_id(org_id);
		summary.setCreate_date(DateUtil.getNowDate(DateUtil.TIME_FMT));
		summary.setOpt_user_id(user_id);
		
		int n = summaryDao.addSummary(summary);
		if(n!=1){
			throw new SummaryNotFoundException("系统繁忙，添加失败");
		}
		
		List<Attach> listAttach = new ArrayList<Attach>();
		
		if(summary.getAttach()!=null&&!summary.getAttach().trim().isEmpty()){
			String str=summary.getAttach().trim();
			System.out.println("+++附件+++");
			JSONArray json=JSONArray.fromObject(str);
			//System.out.println("l="+listAttach.size());
        
			for(int i=0;i<json.size();i++){

				//System.out.println("打桩2："+json.get(i));
				Object check=json.get(i);
				if("null".equals(check.toString())){
					continue;
				}
				JSONObject job = json.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
	
				String attach_ext=(String)job.get("attach_ext");
				String attach_path=(String)job.get("attach_path");
				String attach_size=(String)job.get("attach_size");
				String title=(String)job.get("title");
				Attach attach=new Attach();
				attach.setMessage_no(summary.getMeeting_id());
				attach.setAttach_type(11);
				attach.setTitle(title);
				attach.setAttach_ext(attach_ext);
				attach.setAttach_path(attach_path);
				attach.setAttach_size(attach_size);				
				attach.setOrg_id(org_id);
				attach.setOpt_user_id(user_id);
				listAttach.add(attach);
								
			}        
        }
		List<Attach> newListAttach=FileUtil.moveFile(listAttach,"meeting_assist");
			
		if(newListAttach!=null){
		for(Attach attach:newListAttach){
			attachDao.insertAttach(attach);
			}
		}
		return n;
	}

	public int deleteSummary(HttpServletRequest req, String meeting_id) throws SummaryNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new SummaryNotFoundException("请重新登录");
		}
		if(meeting_id==null||meeting_id.trim().isEmpty()){
			throw new SummaryNotFoundException("请选择删除的会议室");
		}
		int n = summaryDao.deleteSummary(meeting_id,org_id);
		if(n!=1){
			throw new SummaryNotFoundException("系统繁忙，删除失败");
		}
		return n;
	}

	public int updateSummary(HttpServletRequest req, Summary summary)
			throws SummaryNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		String user_id = (String)session.getAttribute("id");
		if(org_id==null){
			throw new SummaryNotFoundException("请重新登录");
		}
		if(summary==null){
			throw new SummaryNotFoundException("请填写会议室信息");
		}
				
		if(summary.getSummary_name()==null||summary.getSummary_name().trim().isEmpty()){
			throw new SummaryNotFoundException("请填写名称");
		}
		if(summary.getMeeting_id()==null||summary.getMeeting_id().trim().isEmpty()){
			throw new SummaryNotFoundException("请选择会议");
		}	
		
		SummaryParam oldSummary = summaryDao.findSummaryById(summary.getMeeting_id(),org_id);
		if(oldSummary==null){
			throw new SummaryNotFoundException("系统繁忙，修改失败");
		}
		
		Summary newSummary = new Summary();
		if(!summary.getSummary_name().equals(oldSummary.getSummary_name())){
			newSummary.setSummary_name(summary.getSummary_name());
		}
		if(!summary.getSummary_des().equals(oldSummary.getSummary_des())){
			newSummary.setSummary_des(summary.getSummary_des());
		}
				
		newSummary.setOpt_user_id(user_id);		
		newSummary.setMeeting_id(summary.getMeeting_id());
		newSummary.setOrg_id(org_id);
		int n = summaryDao.updateSummary(newSummary);
		if(n!=1){
			throw new SummaryNotFoundException("系统繁忙，稍后再试");
		}
		
		
		List<Attach> listAttach = new ArrayList<Attach>();
		
		if(summary.getAttach()!=null&&!summary.getAttach().trim().isEmpty()){
			String str=summary.getAttach().trim();
			//System.out.println(str);
			JSONArray json=JSONArray.fromObject(str);
			//System.out.println("l="+listAttach.size());
        
			for(int i=0;i<json.size();i++){

				//System.out.println("打桩2："+json.get(i));
				Object check=json.get(i);
				if("null".equals(check.toString())){
					continue;
				}
				JSONObject job = json.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
	
				String attach_ext=(String)job.get("attach_ext");
				String attach_path=(String)job.get("attach_path");
				String attach_size=(String)job.get("attach_size");
				String title=(String)job.get("title");
				Attach attach=new Attach();
				attach.setMessage_no(summary.getMeeting_id());
				attach.setAttach_type(11);
				attach.setTitle(title);
				attach.setAttach_ext(attach_ext);
				attach.setAttach_path(attach_path);
				attach.setAttach_size(attach_size);				
				attach.setOrg_id(org_id);
				attach.setOpt_user_id(user_id);
				listAttach.add(attach);
								
			}        
        }
		List<Attach> newListAttach=FileUtil.moveFile(listAttach,"meeting_assist");
			
		if(newListAttach!=null){
		for(Attach attach:newListAttach){
			attachDao.insertAttach(attach);
			}
		}
		
		return n;
	}

	public SummaryParam findSummaryById(HttpServletRequest req, String summary_id)
			throws SummaryNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new SummaryNotFoundException("请重新登录");
		}
		if(summary_id==null||summary_id.trim().isEmpty()){
			throw new SummaryNotFoundException("会议室不存在");
		}
		SummaryParam summary = summaryDao.findSummaryById(summary_id,org_id);
		if(summary==null){
			throw new SummaryNotFoundException("系统繁忙，查询失败");
		}
		return summary;
	}

	public List<SummaryParam> findSummaryByParam(HttpServletRequest req,FindSummaryParam param)
			throws SummaryNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new SummaryNotFoundException("请重新登录");
		}
		Integer page = param.getPage();
		Integer limit = param.getLimit();
		Integer start = null;
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}		
		param.setStart(start);	
		param.setOrg_id(org_id);
		List<SummaryParam> list = summaryDao.findSummaryByParam(param);
		return list;
	}

	public String countSummaryByParam(HttpServletRequest req, FindSummaryParam param)
			throws SummaryNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new SummaryNotFoundException("请重新登录");
		}
		param.setOrg_id(org_id);
		String count = summaryDao.countSummaryByParam(param);
		return count;
	}

	
}
